﻿

using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Oliving.Edge.Model.Dto;
using System.Linq.Expressions;
using Oliving.Edge.Common.Extensions;

namespace Oliving.Edge.Controllers
{
    [ApiController]
    [Route("api/DTSN_ShangWeiJi")]
    [Authorize(Permissions.Name)]
    public class DTSN_ShangWeiJiController : OlivingControll
    {
        IMapper _mapper;
        private readonly IDTSN_ShangWeiJiServices _DTSN_ShangWeiJiServices;
        private readonly ILogger<DTSN_ShangWeiJiController> _logger;

        public DTSN_ShangWeiJiController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<DTSN_ShangWeiJiController> logger,
                IDTSN_ShangWeiJiServices DTSN_ShangWeiJiServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _DTSN_ShangWeiJiServices = DTSN_ShangWeiJiServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<DTSN_ShangWeiJiDto>> Get(int id)
        {
            var data = new MessageModel<DTSN_ShangWeiJiDto>();
            DTSN_ShangWeiJi model = await _DTSN_ShangWeiJiServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<DTSN_ShangWeiJiDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }




        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/GetList")]
        public async Task<MessageModel<List<DTSN_ShangWeiJiDto>>> GetList()
        {
            var data = new MessageModel<List<DTSN_ShangWeiJiDto>>();
            List<DTSN_ShangWeiJi> list = await _DTSN_ShangWeiJiServices.Query();
            list = list.FindAll(F => F.SWJ_State != -1);
            if (list == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<List<DTSN_ShangWeiJiDto>>(list);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }



        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<DTSN_ShangWeiJiPageDto>>> GetPageList(
            int page = 1, int pageSize = 20,
            string sidx = "", string order = "",
            string key = "")
        {
            Expression<Func<DTSN_ShangWeiJi, bool>> whereExpression = w => true && (w.SWJ_State != -1);

            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                whereExpression = whereExpression.And(d =>
                        d.SWJ_Name.Contains(key) ||
                        d.SWJ_Code.Contains(key) ||
                        d.SWJ_Address.Contains(key) ||
                        d.SWJ_IP.Contains(key) ||
                        d.SWJ_Remark.Contains(key)
                    );
            }


            string colOrder = "";
            if (!string.IsNullOrEmpty(sidx))
            {
                if (order.Contains("desc"))
                {
                    order = " desc";
                }
                else
                {
                    order = " asc";
                }

                colOrder = sidx + " " + order;
            }
            var data = await _DTSN_ShangWeiJiServices.QueryPageList(whereExpression, page, pageSize, colOrder);

            return new MessageModel<PageModel<DTSN_ShangWeiJiPageDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] DTSN_ShangWeiJiDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && dto.ID > 0)
            {
                DTSN_ShangWeiJi model = await _DTSN_ShangWeiJiServices.QueryById(dto.ID);
                if (model != null)
                {
                    model.SWJ_Name = dto.SWJ_Name;
                    model.SWJ_Code = dto.SWJ_Code;
                    model.SWJ_Address = dto.SWJ_Address;
                    model.SWJ_IP = dto.SWJ_IP;
                    model.SWJ_Port = dto.SWJ_Port;
                    model.SWJ_Remark = dto.SWJ_Remark;
                    model.SWJ_State = dto.SWJ_State;


                    data.success = await _DTSN_ShangWeiJiServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.ID.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(DTSN_ShangWeiJi dto)
        {
            var data = new MessageModel<string>();
            int id = await _DTSN_ShangWeiJiServices.Add(dto);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var userDetail = await _DTSN_ShangWeiJiServices.QueryById(id);
                userDetail.SWJ_State = -1; //?????????????
                data.success = await _DTSN_ShangWeiJiServices.Update(userDetail);
                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = userDetail?.ID.ObjToString();
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {

                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    DTSN_ShangWeiJi model = new DTSN_ShangWeiJi();
                    model.SWJ_State = -1;   //       ???????????????????
                    string whereStr = " ID in (" + ids + ")";
                    List<string> colList = new List<string>();
                    colList.Add("SWJ_State"); //    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _DTSN_ShangWeiJiServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                if (false)
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _DTSN_ShangWeiJiServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }




    }
}

